package com.cqut.hfh.ordering.service.Impl;

import com.cqut.hfh.ordering.pojo.*;
import com.cqut.hfh.ordering.service.IFoodTypeService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.li.mybatisutil.dao.base.Dao;
import com.cqut.li.mybatisutil.dao.base.SearchDao;
import com.cqut.li.mybatisutil.entity.base.Entity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * create by  admin on 2019/6/25
 */
@Service
public class FoodTypeServiceImpl implements IFoodTypeService {
    @Resource
    private SearchDao searchDao;
    @Resource
    private Dao dao;

    @Override
    public Integer getCount() {
        return searchDao.sqlCount(Entity.getTableName(FoodType.class));
    }

    @Override
    public List<Map<String, Object>> getAllFoodType(String limit, String page) {
        String con = " 1 = 1 limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        List<Map<String, Object>> foodType_list
                = dao.findByCondition(new String[]{
                        "type_id",
                        "type_name",
                       // "sort",
                        "isva",
                        "optime",
                        //"oper"
                         },
                con,
                FoodType.class);

        return foodType_list;
    }

    @Override
    public void AddType(FoodType foodType) {
        Date date=new Date();
        foodType.setOptime(date);
        foodType.setType_id(EntityIdutils.getId());
        dao.save(foodType);
    }

    @Override
    public List<Map<String, Object>> getTypeByName(String str, String page, String type_name) {
        String con = " type_name like '%" + type_name + "%' limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(str) + "," + str ;
        List<Map<String, Object>> type_list
                = dao.findByCondition(new String[]{
                        "type_id",
                        "type_name",
                        // "sort",
                        "isva",
                        "optime",
                        //"oper"
                        },
                con,
                FoodType.class);
        System.out.println(type_list.get(0).get(0));
        System.out.println(type_list.get(0).get(1));
        return type_list;
    }

    @Override
    public Map<String, Object> findById(String type_id) {
        return dao.getByID(type_id, FoodType.class).toMap();
    }

    @Override
    public void updateType(FoodType foodType) {
        Date date=new Date();
        foodType.setOptime(date);
        dao.updatePropByID(foodType, foodType.getType_id());

    }

    @Override
    public void delById(String type_id) {
        dao.deleteByID(type_id, FoodType.class);
        dao.deleteByCondition(" type_id='" + type_id + "'", FodFood.class);

    }

    @Override
    public List<FoodType> getAllType() {
        List<FoodType> foodTypes = dao.getByCondition(null, FoodType.class);
        return foodTypes;
    }


}
